/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2022-10-27
 * Time: 23:28
 */
class MyCircularDeque {
    public int[] arr;
    public int usedSize;
    public int head;
    public int tail=-1;
    public MyCircularDeque(int k) {
        this.arr=new int[k];
    }
    public boolean insertFront(int value) {
        if(isFull()){
            return false;
        }
        //插入时要考虑为空的时候
        if(isEmpty()){
            tail=head=0;
        }else{
            if(head==0){
                head=arr.length-1;
            }else{
                this.head--;
            }
        }
        arr[head]=value;
        this.usedSize++;
        return true;
    }

    public boolean insertLast(int value) {
        if(isFull()){
            return false;
        }
        //插入时要考虑为空的时候
        if(isEmpty()){
            head=tail=0;
        }else{
            if(tail==arr.length-1){
                tail=0;
            }else{
                tail++;
            }
        }

        this.arr[tail]=value;
        this.usedSize++;
        return true;
    }

    public boolean deleteFront() {
        if(isEmpty()){
            return false;
        }
        //删除时要考虑只有一个元素的情况
        if(this.usedSize==1){
            head=tail=0;
            this.usedSize--;
            return true;
        }
        if(head==arr.length-1){
            head=0;
        }else{
            this.head++;
        }
        this.usedSize--;
        return true;
    }

    public boolean deleteLast() {
        if(isEmpty()){
            return false;
        }
        //删除时要考虑只有一个元素的情况
        if(this.usedSize==1){
            head=tail=0;
            this.usedSize--;
            return true;
        }
        if(tail==0){
            tail=arr.length-1;
        }else{
            this.tail--;
        }
        this.usedSize--;
        return true;
    }

    public int getFront() {
        if(isEmpty()){
            return -1;
        }
        return arr[head];
    }

    public int getRear() {
        if(isEmpty()){
            return -1;
        }
        return arr[tail];
    }

    public boolean isEmpty() {
        return this.usedSize==0;
    }

    public boolean isFull() {
        return this.usedSize==arr.length;
    }
}
